package com;

import entity.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.log4j.lf5.util.Resource;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.InputStream;

@WebServlet(name = "LoginServlet", value = "/LoginServlet")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        InputStream is = Resources.getResourceAsStream("Mybatis-config.xml");
        SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(is);
        SqlSession sqlSession = ssf.openSession();
        HttpSession session = request.getSession();
        String act=request.getParameter("act");
        if(act.equals("login")){
            RequestDispatcher requestDispatcher = request.getRequestDispatcher("/Login.jsp");
            User user = new User();
            user.setUsername(request.getParameter("username"));
            String password = sqlSession.selectOne("selectUser",user);
            if(password==null){
                request.setAttribute("errorMsg","用户名错误");
                requestDispatcher.forward(request,response);
            }
            else if(password.equals(request.getParameter("password"))){
                session.setAttribute("Logined",true);
                response.sendRedirect(request.getContextPath()+"/bookstore?act=list");
            }
            else{
                request.setAttribute("errorMsg","密码错误");
                requestDispatcher.forward(request,response);
            }
        }
        else if(act.equals("register")){
            response.sendRedirect(request.getContextPath()+"/register.jsp");
        }
        else if(act.equals("apply")){
            String applyUsername = request.getParameter("applyUsername");
            String applyPassword = request.getParameter("applyPassword");
            String repeatPassword = request.getParameter("repeatPassword");
            User user = new User();
            user.setUsername(applyUsername);
            if(!applyPassword.equals(repeatPassword)){
                request.setAttribute("ErrorMsg","两次输入密码不同！！！");
                RequestDispatcher requestDispatcher = request.getRequestDispatcher("/register.jsp");
                requestDispatcher.forward(request,response);
            }
            else if(sqlSession.selectOne("selectUser",user)!=null){
                request.setAttribute("ErrorMsg","该用户名已存在！！！");
                RequestDispatcher requestDispatcher = request.getRequestDispatcher("/register.jsp");
                requestDispatcher.forward(request,response);
            }
            else{
                user.setPassword(applyPassword);
                sqlSession.insert("insertUser",user);
                sqlSession.commit();
                response.sendRedirect(request.getContextPath()+"/Login.jsp");
            }
        }
        sqlSession.close();
    }
}
